﻿<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="NoticeWebpart.aspx.vb" Inherits="PubProject.NoticeWebpart" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>消息中心</title>
    <link href="/PubProject/css/portal.css" rel="stylesheet" type="text/css" />
    <link href="/PubProject/css/msg.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="/_common/scripts/global.js"></script>
    <script type="text/javascript" src="/PubProject/js/Public.js"></script>
    <script src="/PubProject/js/jquery.js" type="text/javascript"></script>
    <script src="/ekp/js/common.js" type="text/javascript"></script>
    <script type="text/javascript">
        var readInterval=<%= ReadInterval %>;
        var lastReadTime='<%=ReadlastReadTime%>';
        var ntcCount=<%=NoticeCount%>;
        var reqCount=<%=RequestCount%>;
    </script>
    <script type="text/javascript">
        //读取服务器定时器
        var readTimer = false;
        //页面加载初始化
        window.onload = function() {
            if (!readTimer) {
                readTimer = window.setInterval(getMessage, readInterval * 60 * 1000);
            }
        }
        //点击消息
        function clickMessage(mode, oid, url,typeguid) {
            var sPath = "/PubProject/MYDESK/Redirect.aspx?oid=" + escape(oid)  + "&url=" + escape(url);           
            openMyWin(sPath);
            switch (mode) {
                case 0:
                    $("#" + oid).remove();
                    ntcCount--;
                    $("#spanNtcCount").text(ntcCount);
            }
        }
        //显示/隐藏待办
        function showRequest(s) {
            if (s) {
                $("#msg_row_wrapper").find(".msg_request").show();
                $("#lblRequest").attr("title", "点击隐藏待办");
            }
            else {
                $("#msg_row_wrapper").find(".msg_request").hide();
                $("#lblRequest").attr("title", "点击显示待办");
            }
            try {
                if (setTop) {
                    setTop(0);
                }
            }
            catch (e) { }
        }
        //显示/隐藏通知
        function showNotice(s) {
            if (s) {
                $("#msg_row_wrapper").find(".msg_notice").show();
                $("#lblNotice").attr("title", "点击隐藏通知");
            }
            else {
                $("#msg_row_wrapper").find(".msg_notice").hide();
                $("#lblNotice").attr("title", "点击显示通知");
            }
            try {
                if (setTop) {
                    setTop(0);
                }
            }
            catch (e) { }
        }
        //鼠标覆盖
        function mouseOver(area) {
            $(area).addClass("msg_hover");
        }
        //鼠标移开
        function mouseOut(area) { 
            $(area).removeClass("msg_hover");
        }
        //消息记录模板，用于读取到新消息后在界面添加
        var rowtemplate = '<div class="msg_row msg_{0} msg_emergency{1}" id="{2}" onclick="clickMessage({3},\'{2}\',\'{4}\',\'{5}\');"' +
                                        ' onmouseover="mouseOver(this);" onmouseout="mouseOut(this);">{6}</div>';
        //读取服务器新消息
        function getMessage() {
            //读取地址
            var path = "/PubProject/DSS/DSS_XMLHTTP.aspx?ywtype=GetMessage";
            //Ajax请求
            $.ajax({ cache: false, url: path, data: { lasttime: lastReadTime }, dataType: "json", success: function(res) {
                //成功
                if (res.data.length > 0) {
                    //有新消息
                    $(res.data).each(function(i, msg) {
                        //利用模板产生消息记录到界面显示
                        var html = rowtemplate.replace(/\{0\}/g, (msg.type == 1 ? "request" : "notice"))
                        .replace(/\{1\}/g, msg.emergency)
                        .replace(/\{2\}/g, msg.guid)
                        .replace(/\{3\}/g, msg.type)
                        .replace(/\{4\}/g, msg.url)
                        .replace(/\{5\}/g, msg.typeguid)
                        .replace(/\{6\}/g, $.htmlEncode(msg.subject));
                        var newrow = $(html);
                        var first = $(".msg_row:first");
                        if (first.length > 0) {
                            first.before(newrow);
                        } else {
                            $("#msg_row_wrapper").prepend(newrow);
                        }
                        switch (msg.type) {
                            case 0:
                                //通知
                                if (!$("#chkNotice").attr("checked")) {
                                    newrow.hide();
                                }
                                ntcCount++;
                                $("#spanNtcCount").text(ntcCount);
                                break;
                            case 1:
                                //待办
                                if (!$("#chkRequest").attr("checked")) {
                                    newrow.hide();
                                }
                                reqCount++;
                                $("#spanReqCount").text(reqCount);
                                break;
                        }
                    });
                }
                lastReadTime = res.lasttime;
            }, error: function(a, b, c) {
                //读取错误,关闭读取定时器
                window.clearInterval(readTimer);
            }
            })
        }
        function wrap(obj) {
            $(obj).css({ borderWidth: 1, margin: 0 });
        }
        function unwrap(obj) {
            $(obj).css({ borderWidth: 0, margin: 1 });
        }
    </script>
</head>
<body style="padding:0px;overflow:hidden">
    <form id="form1" runat="server">
        <table cellpadding="0" cellspacing="0" style="width:100%; height:100%" <% If Me.IsAutoScroll Then%>onmouseover="stopScroll()" onmouseout="startScroll()"<% End If%>>
            <tr>
                <td style="height:25px" class="msg_head">
                    <table cellpadding="0" cellspacing="0" style="text-align:left; width:auto; white-space:nowrap; overflow:hidden">
                        <tr>
                            <td><input id="chkRequest" onclick="showRequest(this.checked);" type="checkbox" checked="checked" /></td>
                            <td style="padding-right:5px;white-space:nowrap; overflow:hidden"><label id="lblRequest" class="msg_request" for="chkRequest" style=" cursor:pointer; padding-left:20px; background-position:0px 0px" title="点击隐藏待办">待办(<span  id="spanReqCount"><%=RequestCount%></span>)</label></td>
                        </tr>
                        <tr>
                            <td><input id="chkNotice" onclick="showNotice(this.checked);" type="checkbox" checked="checked" /></td>
                            <td style="white-space:nowrap; overflow:hidden"><label id="lblNotice" class="msg_notice" for="chkNotice" style="cursor:pointer; padding-left:20px; background-position:0px 0px" title="点击隐藏通知">通知(<span id="spanNtcCount"><%=NoticeCount%></span>)</label></td>
                        </tr>
                    </table>
                     <% If Me.IsAutoScroll Then%>
                    <div style="position:absolute; top:20px; right:1px; cursor:pointer" >
                        <img alt="上一页" src="/PubProject/images/triangle_up.png"  onclick="moveDown()" style=" margin:1px; border:solid 0px #e5e5e5" onmouseover="wrap(this)"  onmouseout="unwrap(this)"/>
                        <img alt="下一页" src="/PubProject/images/triangle_down.png"  onclick="moveUp()"  style=" margin:1px; border:solid 0px #e5e5e5" onmouseover="wrap(this)"  onmouseout="unwrap(this)" />
                    </div>
                    <% End If%>
                </td>
            </tr>
            <tr>
                <td style="vertical-align:top;">
                    <div id="msg_row_wrapper" class="msg_row_wrapper" style="position:relative; overflow:<%=IIf(Me.IsAutoScroll,"hidden","auto")%>">
                        <% If Me.IsAutoScroll Then%>
                            <div id="scrollArea" style="position:absolute">
                        <% End If%>
                        <asp:Repeater runat="server" ID="rptNotice">
                            <ItemTemplate>
                                <div class="msg_row msg_<%# IIf(CType(Eval("MessageMode"), Integer) = 1, "request", "notice")%> msg_emergency<%#Eval("Emergency")%>" style="font-family:'宋体'"
                                    id="<%# Eval("MessageGUID") %>" onclick="clickMessage(<%#Eval("MessageMode")%>,'<%#Eval("MessageGUID")%>','<%#Eval("Url")%>','<%#Eval("MessageTypeGUID")%>');" onmouseover="mouseOver(this);" onmouseout="mouseOut(this);">
                                   <%#HttpUtility.HtmlEncode(Eval("Subject"))%>
                                </div>
                            </ItemTemplate>
                        </asp:Repeater>
                        <% If Me.IsAutoScroll Then%>
                            </div>
                            <script type="text/javascript">
                                var intDelay = 3000; //开始滚动前的延迟
                                
                                var scrolltimer = false;
                                var scrollArea = $("#scrollArea");
                                var wrapperHeight = $("#msg_row_wrapper").height();
                                var top = 0;
                                var _inited = false;
                                
                                //滚动函数
                                function scrollUp() {
                                    top --;
                                    if (scrollArea.height() + top <= wrapperHeight) {//滚动到底，暂停一段时间后重置滚动
                                        stopScroll();
                                        _inited = false;
                                        window.setTimeout(function() {//暂停
                                            setTop(0);
                                            window.setTimeout(initScroll, intDelay); //重置滚动
                                        }, 3000);
                                    }
                                    else {
                                        setTop(top);
                                    }
                                }
                                function setTop(t) {
                                    top = t;
                                    if (scrollArea.height() + top <= wrapperHeight) {
                                        top = wrapperHeight - scrollArea.height();
                                    }
                                    if (top > 0) {
                                        top = 0;
                                    }
                                    scrollArea.css("top", top);
                                }
                                //暂停滚动
                                function stopScroll() {
                                    try {
                                        window.clearInterval(scrolltimer);
                                        scrolltimer = false;
                                    }
                                    catch (e) { }
                                }
                                //恢复滚动
                                function startScroll() {
                                    if (_inited && !scrolltimer) {
                                        scrolltimer = window.setInterval(scrollUp, 50);
                                    }
                                }
                                //初始化滚动
                                function initScroll() {
                                    wrapperHeight = $("#msg_row_wrapper").height();
                                    stopScroll();
                                    if (scrollArea.height() > wrapperHeight) {
                                        _inited = true;
                                        startScroll();
                                    }
                                }

                                window.setTimeout(initScroll, intDelay);
                                window.onresize = initScroll;
                                
                                //点击上滚动
                                function moveUp() {
                                    wrapperHeight = $("#msg_row_wrapper").height();
                                    move(wrapperHeight);
                                }
                                //点击下滚动
                                function moveDown() {
                                    wrapperHeight = $("#msg_row_wrapper").height();
                                    move(wrapperHeight * -1);
                                }
                                function move(x) {
                                    setTop(top-x);
                                }
                            </script>
                        <% End If%>
                    </div>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
